var t = require;
var i = module;
var o,
    s = (o = t("astar")) && o.__esModule ? o : {default: o};
cc.Class({
    extends: cc.Component,
    properties: {
        prefabWord: cc.Prefab,
        prefabWordCi: cc.Prefab,
        prefabWordDian: cc.Prefab,
        prefabWordQiang: cc.Prefab,
        prefabWordGui: cc.Prefab,
        prefabWordBi: cc.Prefab,
        prefabWordMen: cc.Prefab,
        mcamera: cc.Camera,
        mapRoot: cc.Node
    },
    onLoad: function onLoad() {
        (window.packMan = this), (cc.director.getCollisionManager().enabled = !0);
    },
    start: function start() {
        var t = this;
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this),
            this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this),
            this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this),
            this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this),
            (this.wordWidth = 65),
            (this.wordHeight = 65),
            (this.level = lGlobal.curLevel),
            (this.wordTotal = []),
            (this.wordMap = []);
        var i = lGlobal.lvCfg.json[lGlobal.curModel][lGlobal.curLevel],
            o = lGlobal.modelBundles[lGlobal.curModel];
        if (o) {
            var s = i.lv;
            o.load("lv/" + s, cc.TextAsset, function (i, o) {
                if (i) console.log(i);
                else {
                    t.mapData = [];
                    for (var s = o.text.split("\r\n"), e = 0; e < s.length; e++) {
                        s[e] && s[e].length > 0 && (t.mapData[e] = s[e].split(","));
                    }
                    t.initMap();
                }
            });
        }
        (this.ccLeft = 0), (this.ccTop = 0), (this.spacingX = 0), (this.spacingY = 0), (this.touchStartPos = null);
    },
    initMap: function initMap() {
        (this.ctlNode = cc.instantiate(this.prefabWord)),
            (this.ctlNode.group = "player"),
            (this.ctlNode.parent = this.mapRoot),
            (this.mapRoot.width = this.mapData[0].length * this.wordWidth),
            (this.mapRoot.height = this.mapData.length * this.wordHeight);
        for (var t = 0; t < this.mapData.length; t++) {
            this.wordMap[t] = [];
            for (var i = 0; i < this.mapData[t].length; i++) {
                if (this.mapData[t][i] && this.mapData[t][i].length > 0)
                    if ("出" == this.mapData[t][i])
                        (this.ctlNode.position = this.getPixelPosByLogicPos(i, t)), (this.ctlNode.group = "player");
                    else if (this.mapData[t][i].indexOf("刺") >= 0) {
                        var o = cc.instantiate(this.prefabWordCi);
                        (o.group = "map"), (o.parent = this.mapRoot), (o.position = this.getPixelPosByLogicPos(i, t));
                        var e = o.getComponent("PMWordBase");
                        e.setType(this.mapData[t][i]), (this.wordMap[t][i] = e), this.wordTotal.push(e);
                    } else if (this.mapData[t][i].indexOf("电") >= 0) {
                        var a = cc.instantiate(this.prefabWordDian);
                        (a.group = "map"), (a.parent = this.mapRoot), (a.position = this.getPixelPosByLogicPos(i, t));
                        var n = a.getComponent("PMWordBase");
                        n.setType(this.mapData[t][i]), (this.wordMap[t][i] = n), this.wordTotal.push(n);
                    } else if (this.mapData[t][i].indexOf("墙") >= 0) {
                        var r = cc.instantiate(this.prefabWordQiang);
                        (r.group = "map"), (r.parent = this.mapRoot), (r.position = this.getPixelPosByLogicPos(i, t));
                        var h = r.getComponent("PMWordBase");
                        h.setType(this.mapData[t][i]), (this.wordMap[t][i] = h), this.wordTotal.push(h);
                    } else if (this.mapData[t][i].indexOf("金") >= 0 || this.mapData[t][i].indexOf("银") >= 0) {
                        var c = cc.instantiate(this.prefabWordBi);
                        (c.group = "map"), (c.parent = this.mapRoot), (c.position = this.getPixelPosByLogicPos(i, t));
                        var d = c.getComponent("PMWordBase");
                        d.setType(this.mapData[t][i]), (this.wordMap[t][i] = d), this.wordTotal.push(d);
                    } else if (this.mapData[t][i].indexOf("鬼") >= 0) {
                        var p = cc.instantiate(this.prefabWordGui);
                        (p.group = "map"), (p.parent = this.mapRoot), (p.position = this.getPixelPosByLogicPos(i, t));
                        var l = p.getComponent("PMWordBase");
                        l.setType(this.mapData[t][i]), (this.wordMap[t][i] = l), this.wordTotal.push(l);
                    } else if (this.mapData[t][i].indexOf("门") >= 0) {
                        var g = cc.instantiate(this.prefabWordMen);
                        (g.group = "map"), (g.parent = this.mapRoot), (g.position = this.getPixelPosByLogicPos(i, t));
                        var u = g.getComponent("PMWordBase");
                        u.setType(this.mapData[t][i]), (this.wordMap[t][i] = u), this.wordTotal.push(u);
                    }
            }
        }
        (this.ctlWord = this.ctlNode.getComponent("PMWordBase")), this.ctlWord.setType("我_0_0_false_pmWo_null");
        for (var f = [], m = 0; m < this.mapData.length; m++) {
            f[m] = [];
            for (var P = 0; P < this.mapData[m].length; P++) {
                var w =
                    this.mapData[m][P].indexOf("墙") >= 0 ||
                    this.mapData[m][P].indexOf("电") >= 0 ||
                    this.mapData[m][P].indexOf("刺") >= 0;
                f[m][P] = w ? 0 : 1;
            }
        }
        var v = new s.default.Graph(f, {diagonal: !1});
        this.graphDiagonal = v;
    },
    onTouchStart: function onTouchStart(t) {
        var i = t.getLocation();
        this.touchStartPos = i;
    },
    onTouchMove: function onTouchMove() {
        this.inDrag = !0;
    },
    onTouchEnd: function onTouchEnd(t) {
        this.inDrag = !1;
        var i = t.getLocation();
        if (this.touchStartPos) {
            var o = i.sub(this.touchStartPos),
                s = o.x,
                e = o.y;
            (0 == s && 0 == e) ||
                (Math.abs(s) > Math.abs(e)
                    ? this.ctlWord.startMove(s / Math.abs(s), 0)
                    : this.ctlWord.startMove(0, e / Math.abs(e)));
        }
        this.touchStartPos = null;
    },
    getLogicPosByPixelPos: function getLogicPosByPixelPos(t) {
        var i = 0 - this.mapRoot.width * this.mapRoot.anchorX + this.wordWidth / 2 + this.ccLeft,
            o = this.mapRoot.height - this.mapRoot.height * this.mapRoot.anchorY - this.wordHeight / 2 + this.ccTop,
            s = t.x - i,
            e = o - t.y,
            a = Math.round(s / (this.wordWidth + this.spacingX)),
            n = Math.round(e / (this.wordHeight + this.spacingY));
        return cc.v2(a, n);
    },
    getWordByLogicPos: function getWordByLogicPos(t, i) {
        for (var o = null, s = 0; s < this.wordTotal.length; s++) {
            if (!this.wordTotal[s].hided) {
                var e = this.getLogicPosByPixelPos(this.wordTotal[s].node.position);
                if (e.x == t && e.y == i) {
                    o = this.wordTotal[s];
                    break;
                }
            }
        }
        return o;
    },
    getPixelPosByLogicPos: function getPixelPosByLogicPos(t, i) {
        var o = 0 - this.mapRoot.width * this.mapRoot.anchorX + this.wordWidth / 2 + this.ccLeft,
            s = this.mapRoot.height - this.mapRoot.height * this.mapRoot.anchorY - this.wordHeight / 2 + this.ccTop,
            e = t * (this.wordWidth + this.spacingX) + o,
            a = s - i * (this.wordHeight + this.spacingY);
        return cc.v2(e, a);
    },
    update: function update() {
        if (this.ctlNode) {
            var t = cc.Vec3.ZERO;
            if (
                (cc.Vec3.lerp(t, this.mcamera.node.position, this.ctlNode.position, 0.06),
                (this.mcamera.node.position = t),
                0 != this.ctlWord.spdX || 0 != this.ctlWord.spdY)
            )
                for (
                    var i = this.getLogicPosByPixelPos(this.ctlWord.node.position),
                        o = this.getWordByLogicPos(i.x, i.y - 1),
                        s = this.getWordByLogicPos(i.x, i.y + 1),
                        e = this.getWordByLogicPos(i.x + 1, i.y),
                        a = [o, s, this.getWordByLogicPos(i.x - 1, i.y), e],
                        n = 0;
                    n < a.length;
                    n++
                ) {
                    var r = a[n];
                    r && (("鬼" != r.word && "刺" != r.word && "电" != r.word) || r.triggerEvent(this.ctlNode, i));
                }
        }
    }
});
